const app = getApp()
const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({
  data: {
    banner:[],
    category: [],
    article: [],
    package: [],
    product: [],
    city: [],
    city_id: '',
    city_name: '',
    searchKey: '',
  },
  onLoad(options){
    let that = this;
    that.getCityList();
  },
  onShow: function () {
    let that = this;
    that.setData({
      city_id: wx.getStorageSync('city_id'),
      city_name: wx.getStorageSync('city_name') || '深圳市',
    })
    qqmapsdk = new QQMapWX({
      key: 'KUTBZ-7MFCP-5RUDQ-VXJ7V-CUIIE-46B65' //这里自己的key秘钥进行填充
    });
    app.confirmLaunch(function(){
      that.getInfo();
    })
  },
  getCityList(){
    let that = this;
    app.request('city/getCityList',{},'GET',false,function(res){
      that.setData({
        city: res.data.data,
      })
    })
  },
  getInfo(e){
    let that = this;
    let city_id = wx.getStorageSync('city_id');
    app.request('index/index', { city_id },'GET',false,function(res){
      that.setData({
        pageShow: true,
        banner: res.data.data.banner || [],
        category: res.data.data.category || [],
        article: res.data.data.article || [],
        package: res.data.data.package || [],
        product: res.data.data.product || [],
      })
      if(res.data.data.phone){
        wx.setStorageSync('phone', res.data.data.phone)        
      }
    })
  },
  getFormid(e){
    app.getFormid(e);
  },
  skipPage:function(e){
    app.skipPage(e);
  },
  searchInput(e){
    this.setData({
      searchKey: e.detail.value,
    })
  },
  search(e){
    let that = this;
    wx.navigateTo({
      url: '/pages/list/list?value='+that.data.searchKey,
    })
  },
  cityShow(){
    let that = this;
    that.setData({
      cityShow: true,
    })
  },
  cityHide(e){
    let that = this;
    that.setData({
      cityShow: false,
    })
  },
  reLocal(e){
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            let city_name = res.result.ad_info.city.substring(0, res.result.ad_info.city.length-1);
            let address = res.result.address;
            let city = that.data.city.filter((v)=>{
              return city_name == v.title;
            })
            console.log(city)
            wx.setStorageSync('city_id', city[0].id)
            wx.setStorageSync('city_name', city_name)
            that.setData({
              city_name: city_name,
              city_id: city[0].id,
              address: address,
              
            },()=>{
              that.getInfo();
            })
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {

          }
        });
      }
    })
    
  },
  cityChoose(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let city_name = that.data.city[index].title;
    let city_id = that.data.city[index].id;
    wx.setStorageSync('city_name', city_name)
    wx.setStorageSync('city_id', city_id)
    that.setData({
      city_name: city_name,
      city_id: city_id,
      cityShow: false,
    }, () => {
      that.getInfo();
    })
    
  },
  onReachBottom: function () {
    
  },

  onShareAppMessage: function () {
    
  }
})
